package cn.wolfcode.wms.util;

import cn.wolfcode.wms.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.Set;

public class UserContext {


    private static final String USER_IN_SESSION = "USER_IN_SESSION";
    private static final String EXPESSION_IN_SESSION = "EXPESSION_IN_SESSION";


    //获取到一个sesion
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }


    public static void setCurrentUser(Employee emp){
        //判断用户是否纯在,如果不存在就关闭session
        if(emp == null){
            //invalidate()销毁session
            getSession().invalidate();
        }else{
            getSession().setAttribute(USER_IN_SESSION,emp);
        }

    }
    public static void  setExps(Set<String> exps){
        getSession().setAttribute(EXPESSION_IN_SESSION,exps);
    }

    public static Employee getCurrentUser() {
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }
    public static Set<String> getExps(){
        return (Set<String>) getSession().getAttribute(EXPESSION_IN_SESSION);
    }
}

